package edu.palermo.dondeestoy.rest;

import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;

import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;

import org.springframework.http.converter.json.GsonHttpMessageConverter;

import org.springframework.web.client.RestTemplate;

import android.util.Log;


import edu.palermo.dondeestoy.bo.BaseResponse;
import edu.palermo.dondeestoy.bo.Requestclass;


public class ApiService {

	private final static String Tag="ApiService";
	private final static int TIMEOUT=1;
	private static String serverAddress = "";

	public static String getServerAddress() {
		return serverAddress;
	}

	public static void setServerAddress(String serverAddress) {
		ApiService.serverAddress = serverAddress;
	}


	
	
	//public BaseResponse postUpdateLocation(double latitude,double longitude, String device) {
	public boolean postUpdateLocation(double latitude,double longitude, String device) throws Exception {

		Requestclass rq=new Requestclass();		
		Requestclass.Request_UpdateLocation uplocation = rq.new Request_UpdateLocation();		 
		uplocation.setLatitude(latitude);
		uplocation.setLongitude(longitude);
		
		HttpHeaders requestHeaders = new HttpHeaders();
		//requestHeaders.setAccept(Collections.singletonList(new MediaType("application","json")));
		requestHeaders.setContentType(new MediaType("application","json"));
		
		//HttpEntity<?> requestEntity = new HttpEntity<Object>(requestHeaders);
		HttpEntity<Requestclass.Request_UpdateLocation> requestEntity = new HttpEntity<Requestclass.Request_UpdateLocation>(uplocation, requestHeaders);
		
		RestTemplate restTemplate = new RestTemplate();
		restTemplate.getMessageConverters().add(new GsonHttpMessageConverter());
		
		String url = "http://" + serverAddress + "/"+device;			
		
		//ResponseEntity<BaseResponse> responseEntity = restTemplate.exchange(url, HttpMethod.POST, requestEntity, BaseResponse.class);
		try
		{
			restTemplate.exchange(url, HttpMethod.POST, requestEntity, BaseResponse.class);
		}catch(Exception e)
		{
			throw e;
		}
		
		//BaseResponse result = responseEntity.getBody();
		
		return true;
		//return result;
	
	}	
	
	public BaseResponse postUpdateLocationHTTP(double latitude,double longitude, String device,float velocidad,float precision,double altitud,float bearing,String id,String strBattery) 
	{
		Requestclass rq=new Requestclass();		
		Requestclass.Request_UpdateLocation uplocation = rq.new Request_UpdateLocation();		 
		uplocation.setLatitude(latitude);
		uplocation.setLongitude(longitude);
		uplocation.setAltitud(altitud);
		uplocation.setBearing(bearing);
		uplocation.setPrecision(precision);
		uplocation.setVelocidad(velocidad);
		uplocation.setImei(device);
		uplocation.setId(id);
		uplocation.setBattery(strBattery);
		
		HttpHeaders requestHeaders = new HttpHeaders();
		//requestHeaders.setAccept(Collections.singletonList(new MediaType("application","json")));
		requestHeaders.setContentType(new MediaType("application","json"));
		
		//HttpEntity<?> requestEntity = new HttpEntity<Object>(requestHeaders);
		HttpEntity<Requestclass.Request_UpdateLocation> requestEntity = new HttpEntity<Requestclass.Request_UpdateLocation>(uplocation, requestHeaders);
		
		RestTemplate restTemplate = new RestTemplate();
		restTemplate.getMessageConverters().add(new GsonHttpMessageConverter());
		
		String url = "http://" + this.getServerAddress() + "/"+device;			
		Log.d("SERVER", "URL: "+url);	//DEBUG
		
		ResponseEntity<BaseResponse> responseEntity = restTemplate.exchange(url, HttpMethod.POST, requestEntity, BaseResponse.class);
		
		BaseResponse result = responseEntity.getBody();
		
		return result;
	
	}	
}
